/**
 * 采购合同单
 */

app.controller('formSelfCtrl', ['$scope', '$http', FormSelfController]);
function FormSelfController($scope, $http){

	//监听子表数字输入框变更
	$scope.subGrid.numberChange = function(row, field) {
		if (field == 'subPurchasePrice') {
			if($scope.subGrid.sonlist[row]['subPurchaseNum'] && $scope.subGrid.sonlist[row]['subPurchasePrice']) {
				//子件采购总价
				$scope.subGrid.sonlist[row]['purchaseTotalPrice'] = (parseFloat($scope.subGrid.sonlist[row]['subPurchaseNum'])
					*parseFloat($scope.subGrid.sonlist[row]['subPurchasePrice'])).toFixed(2);
			}
			//计算合同金额
			var contractAmount = 0 ;//合同金额
			if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					if($scope.subGrid.sonlist[i].purchaseTotalPrice){
						contractAmount = contractAmount+parseFloat($scope.subGrid.sonlist[i].purchaseTotalPrice);
					}
				}
			}

			//回填合同金额
			$scope.valueMap['contractAmount'] = contractAmount.toFixed(2);
		}
	}


}

app.controller('btnCtrl', ['$scope', '$http', BtnController]);
function BtnController($scope, $http){
	var gridId = $scope.gridId;

	$scope.bindBtn = function(event) {
		switch(event){
			case 'event-to-add': // 新增
			{
				var companyInfoVid = $scope.valueMap['companyInfoVid'];//购买方公司编码
				if(companyInfoVid==undefined || companyInfoVid==null || companyInfoVid==''){
					return layer.msg("请先选择购买方公司名称！");
				}

				var listUkey = [];
				for(var i=0;i<$scope.subGrid.sonlist.length;i++){
					var data = $scope.subGrid.sonlist[i];
					listUkey.push(data.orderProductComponentId);
				}
				$scope.showModalCheckBox('TCG_PurchaseAppointOrder_ItemByContract', 'orderProductComponentId', listUkey,[{code: 'CI.Vname', value:vname}]);

				break;

			}
			case 'event-to-delete': // 删除
			{
				$scope.subGrid.deleteBatch();
				var contractAmount = 0 ;
				if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
					for(var i=0;i<$scope.subGrid.sonlist.length;i++){
						if($scope.subGrid.sonlist[i].purchaseTotalPrice){
							contractAmount = contractAmount+parseFloat($scope.subGrid.sonlist[i].purchaseTotalPrice);
						}
					}
				}
				//回填合同金额
				$scope.valueMap['contractAmount'] = contractAmount.toFixed(2);
				break;
			}
		};
	}
}
/*
 * 子列表控制器
 */
app.controller('subGridCtrl', ['$scope', '$http', function SubGridController($scope, $http){
	/*
	 * 复选框回调
	 */
	$scope.checkbox.afterSelectDatas = function(rowsSelected) {
		for (var i = 0; i < rowsSelected.length; i++) {
			var row = rowsSelected[i];
			var isAdd = true; // 根据现有存在，是否加新行
			for (var j = 0; j < $scope.subGrid.sonlist.length; j++) {
				if ($scope.subGrid.sonlist[j].orderProductComponentId == row.orderProductComponentId) {
					isAdd = false;
				}
			}
			if (isAdd) {
				$scope.subGrid.sonlist.push({
					id: null,
					purchaseAppointOrderId: row.id,
					orderProductComponentId: row.orderProductComponentId,
					appointOrderBid: row.bid,
					vgsbm: row.vgsbm,
					vgsjc: row.vgsjc,
					productCode: row.productCode,
					productName: row.productName,
					codeMerge: row.codeMerge,
					reserveNumber: row.reserveNumber,
					subCode: row.subCode,
					subName: row.subName,
					subCodeMerge: row.subCodeMerge,
					subBomNumber: row.subBomNumber,
					subReserveNumber: row.subReserveNumber,
					subPurchaseNum: row.subReserveNumber,
					subPurchasePrice: row.subBomPrice,
					purchaseTotalPrice:(parseFloat(row.subReserveNumber)*parseFloat(row.subBomPrice)).toFixed(2)
				});
			}
		}

		var contractAmount = 0 ;
		if($scope.subGrid.sonlist!=null && $scope.subGrid.sonlist.length>0){
			for(var i=0;i<$scope.subGrid.sonlist.length;i++){
				if($scope.subGrid.sonlist[i].purchaseTotalPrice){
					contractAmount = contractAmount+parseFloat($scope.subGrid.sonlist[i].purchaseTotalPrice);
				}
			}
		}
		//回填合同金额
		$scope.valueMap['contractAmount'] = contractAmount.toFixed(2);


	}
}]);

